import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _class;

/**
 * DataImportHistory
 * @date 2019-08-21
 */
import React, { Component } from 'react';
import { Bind } from 'lodash-decorators';
import { DEFAULT_DATETIME_FORMAT } from 'utils/constants';
import { createPagination, getResponse } from 'utils/utils';
import SearchForm from "./SearchForm";
import DataTable from "./DataTable";
var DataImportHistory = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), (_class = /*#__PURE__*/function (_Component) {
  _inherits(DataImportHistory, _Component);

  var _super = _createSuper(DataImportHistory);

  function DataImportHistory(props) {
    var _this;

    _classCallCheck(this, DataImportHistory);

    _this = _super.call(this, props);
    _this.searchFormRef = React.createRef();
    _this.state = {
      dataSource: [],
      pagination: false // cachePagination: {}, // 删除后刷新需要之前的分页信息

    };
    return _this;
  }

  _createClass(DataImportHistory, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.handleSearch();
    }
  }, {
    key: "reload",
    value: function () {
      var _reload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var cachePagination;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                cachePagination = this.state.cachePagination;
                _context.next = 3;
                return this.handleSearch(cachePagination);

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function reload() {
        return _reload.apply(this, arguments);
      }

      return reload;
    }()
  }, {
    key: "handleSearch",
    value: function () {
      var _handleSearch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        var pagination,
            queryImportHistory,
            fieldsValue,
            res,
            responseRes,
            _args2 = arguments;
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                pagination = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {};
                this.setState({
                  queryLoading: true
                });
                queryImportHistory = this.props.queryImportHistory;
                fieldsValue = this.searchFormRef.current ? this.searchFormRef.current.props.form.getFieldsValue() : {};
                fieldsValue.creationDateTo = fieldsValue.creationDateTo ? fieldsValue.creationDateTo.format(DEFAULT_DATETIME_FORMAT) : undefined;
                fieldsValue.creationDateFrom = fieldsValue.creationDateFrom ? fieldsValue.creationDateFrom.format(DEFAULT_DATETIME_FORMAT) : undefined;
                _context2.next = 8;
                return queryImportHistory(_objectSpread(_objectSpread({}, pagination), fieldsValue));

              case 8:
                res = _context2.sent;
                responseRes = getResponse(res);

                if (responseRes) {
                  this.setState({
                    dataSource: responseRes.content,
                    pagination: createPagination(responseRes),
                    cachePagination: pagination,
                    queryLoading: false
                  });
                }

              case 11:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function handleSearch() {
        return _handleSearch.apply(this, arguments);
      }

      return handleSearch;
    }() // SearchForm

  }, {
    key: "handleSearchFormSearch",
    value: function () {
      var _handleSearchFormSearch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return this.handleSearch();

              case 2:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function handleSearchFormSearch() {
        return _handleSearchFormSearch.apply(this, arguments);
      }

      return handleSearchFormSearch;
    }() // DataTable

  }, {
    key: "handleTableChange",
    value: function () {
      var _handleTableChange = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(page, filter, sorter) {
        return _regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return this.handleSearch({
                  page: page,
                  sort: sorter
                });

              case 2:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      function handleTableChange(_x, _x2, _x3) {
        return _handleTableChange.apply(this, arguments);
      }

      return handleTableChange;
    }()
  }, {
    key: "handleRecordRestore",
    value: function () {
      var _handleRecordRestore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(record) {
        var onRecordRestore;
        return _regeneratorRuntime.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                onRecordRestore = this.props.onRecordRestore;
                _context5.next = 3;
                return onRecordRestore(record);

              case 3:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));

      function handleRecordRestore(_x4) {
        return _handleRecordRestore.apply(this, arguments);
      }

      return handleRecordRestore;
    }()
  }, {
    key: "handleRecordDelete",
    value: function () {
      var _handleRecordDelete = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(record) {
        var onRecordDelete;
        return _regeneratorRuntime.wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                onRecordDelete = this.props.onRecordDelete;
                this.setState({
                  deleteLoading: true
                });
                _context6.next = 4;
                return onRecordDelete(record);

              case 4:
                _context6.next = 6;
                return this.reload();

              case 6:
                this.setState({
                  deleteLoading: false
                });

              case 7:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6, this);
      }));

      function handleRecordDelete(_x5) {
        return _handleRecordDelete.apply(this, arguments);
      }

      return handleRecordDelete;
    }()
  }, {
    key: "render",
    value: function render() {
      var dataImportStatus = this.props.dataImportStatus;
      var _this$state = this.state,
          dataSource = _this$state.dataSource,
          pagination = _this$state.pagination,
          queryLoading = _this$state.queryLoading,
          deleteLoading = _this$state.deleteLoading;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SearchForm, {
        onSearch: this.handleSearchFormSearch,
        wrappedComponentRef: this.searchFormRef
      }), /*#__PURE__*/React.createElement(DataTable, {
        onRecordRestore: this.handleRecordRestore,
        onRecordDelete: this.handleRecordDelete,
        onChange: this.handleTableChange,
        dataImportStatus: dataImportStatus,
        dataSource: dataSource,
        pagination: pagination,
        queryLoading: queryLoading,
        deleteLoading: deleteLoading
      }));
    }
  }]);

  return DataImportHistory;
}(Component), (_applyDecoratedDescriptor(_class.prototype, "handleSearchFormSearch", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleSearchFormSearch"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleTableChange", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleTableChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRecordRestore", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "handleRecordRestore"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRecordDelete", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "handleRecordDelete"), _class.prototype)), _class));
export default DataImportHistory;